package com.beerq.view;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.beerq.R;
import com.beerq.util.Constants;
import com.beerq.util.WebViewUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class SecondFragment extends Fragment {

    Activity mActivity;

    WebView mWebView;

    //啤酒志的返回到顶部
    private View mLin_back_to_top;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity=getActivity();
        mWebView=(WebView) mActivity.findViewById(R.id.webview_pjz);

        mLin_back_to_top=(mActivity.findViewById(R.id.lin_back_to_top1));
        ProductJsManager productJsManager=new ProductJsManager(getActivity(),mWebView);
        productJsManager.setLin_back_to_top(mLin_back_to_top);
        mWebView.addJavascriptInterface(productJsManager, "android");




        WebViewUtil.initWebView(getActivity(),mWebView);
        String url=Constants.domain+"/piqizhi/index.html";
        mWebView.loadUrl(url);



        mWebView.setWebChromeClient(
                new WebChromeClient() {
                    @Override
                    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                        if("1".equals(message)){
                            mLin_back_to_top.setVisibility(View.VISIBLE);
                        }else {
                            mLin_back_to_top.setVisibility(View.GONE);
                        }
                        result.confirm();
                        return true;
                    }}
        );

        mLin_back_to_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.loadUrl("javascript:icangotop()");
            }
        });

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            ProductJsManager.isShowBackToTop(mWebView);
        }else {
            mLin_back_to_top.setVisibility(View.GONE);
        }
    }



}
